﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace stuManage
{
    /// <summary>
    /// 用于操作数据库的访问类
    /// </summary>
    public class DBHelper
    {
        MySqlConnection connection = new MySqlConnection();
        MySqlCommand command = new MySqlCommand();
        string connectionString = "server=localhost;port=3306;user id=root;password=lxy1756431734;database=student; pooling=true;";

        private void ConnectDataBase()
        {
            connection.ConnectionString = connectionString;//指定连接字符串
            if (connection.State == System.Data.ConnectionState.Closed)
            {
                connection.Open();
            }
        }

        public void ExecuteSQL(string commandString)
        {
            try
            {
                ConnectDataBase();
                command.Connection = connection;//指定连接
                command.CommandText = commandString;//指定要被操作的sql命令
            command.ExecuteNonQuery();//执行
        }
            catch (Exception)
            {

            }
            finally
            {
                CloseConnection();
}

        }
        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        private void CloseConnection()
        {
            connection.Close();
        }

        
    }
}